<?php
require_once(dirname(__FILE__) . '/inc/config.inc.php');
IsModelPriv('goodsorder');
require_once "function.php";

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

$projectInfo = $dosql->GetOne("SELECT * FROM `#@__project` WHERE  id='$id'  AND is_delete=0  ");
if (!is_array($projectInfo)) {
    exit("工单不存在或者被删除");
}


/*
进度：
0 已经撤单
1 报修成功
2 客服已经联系
3 系统已经派单
4 工程师接单
5 维修完成
6 已经评价
*/
$op_uid = $_SESSION['uid'];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>编辑工程</title>
    <link href="templates/style/admin.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="templates/js/jquery.min.js"></script>
    <script type="text/javascript" src="layer/layer.js"></script>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link href="layui/css/layui.css" rel="stylesheet" type="text/css" media="all"/>
    <script src="layui/layui.js"></script>
    <script language="javascript" type="text/javascript" src="DatePicker/WdatePicker.js"></script>

</head>
<style>
    #test-upload-more-lis, #test-upload-more-list2 {
        display: block;
        width: 100%;
        height: auto;
        overflow: auto;
    }

    #test-upload-more-list div, #test-upload-more-list div {
        float: left;
        width: 130px;
        height: 180px;
        margin: 0 10px 10px 0
    }

    #test-upload-more-list img , #test-upload-more-list img {
        width: 130px;
        height: 130px;
    }

    .w20 {
        display: inline-block;
        margin-left: 5px;
        width: 30px !important;
    }

    .w100 {
        display: inline-block;
        margin-left: 5px;
        width: 100px !important;
    }

    .w200 {
        display: inline-block;
        margin-left: 5px;
        width: 200px !important;
    }

    .homepage-bottom .layui-card-body img {
        width: 64px;
        height: 64px;
        border-radius: 50%;
        margin-top: 10px;
    }

    .homepage-bottom .layui-card-body .meida-left {
        display: table-cell;
        padding-right: 10px;
    }

    .homepage-bottom .layui-card-body .meida-right {
        display: table-cell;
        vertical-align: top;
    }

    .homepage-bottom .layui-card-body a:hover {
        background: #F2F2F2;
    }

    .homepage-bottom .layui-card-body {
        background: #fff;
    }

    .layui-card-header .panel-title .layui-icon {
        position: relative;
        left: 0;
        right: 0px;
    }
</style>
<body style="width:890px;">
<div class="layui-fluid">
    <div class="layui-card">
        <h1 class="tit" style="height: 50px; line-height: 50px;">编辑工程</h1>
        <div class="layui-card-header">
            <div class="order_top1">发布人：<?php echo $projectInfo['poster']; ?></div>
            <div class="order_top1 t_fr">创建日期：<?php echo date("Y年m月d日", $projectInfo['post_time']); ?></div>
        </div>
        <div class="layui-card-body layui-layout-body" style=" padding: 15px; ">
            <div class="layui-form layui-col-md12" lay-filter="component-form-group-edit" id="form1">
                <div class="layui-form-item">
                    <input type="hidden" id="id" name="id" value="<?php echo $projectInfo['id']?>"/>
                    <div class="layui-col-sm6"><label class="layui-form-label layui-bg-blue">工程类型<span
                                    class="red">*</span></label>
                        <div id="ordertypebox1" class="layui-input-inline">
                            <select name="task" id="task" lay-filter="task_s">

                                <?php
                                $dosql->Execute("SELECT * FROM `#@__task` WHERE `checkinfo`='1' and is_delete=0  and classid in (6,7) order by orderid ASC", 11);
                                while ($projectInfo_task = $dosql->GetArray(11)) {
                                    $sec = "";
                                    if ($projectInfo['apply_device'] == $projectInfo_task['id']) {
                                        $sec = "selected";
                                    }
                                    echo '<option ' . $sec . ' value="' . $projectInfo_task['id'] . '" >' . $projectInfo_task['title'] . "  " . $projectInfo_task['value'] . $projectInfo_task['unit'] . '</option>';

                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-sm6"><label class="layui-form-label layui-bg-green">工程名称<span
                                    class="red">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" name="projectname" id="projectname"
                                   autocomplete="off" id="contact" placeholder="请输入工程的项目名称"
                                   class="layui-input" value="<?php echo $projectInfo['projectname']; ?>"></div>

                    </div>

                </div>
                <div class="layui-form-item">
                    <div class="layui-col-sm6"><label class="layui-form-label">甲方<span class="red">*</span></label>
                        <div class="layui-input-inline">
                            <input type="text" name="school" id="school"
                                   autocomplete="off" id="contact" placeholder="请输入甲方名称"
                                   class="layui-input" value="<?php echo $projectInfo['school']; ?>">
                        </div>
                    </div>
                    <div class="layui-col-sm6"><label class="layui-form-label">联系人</label>
                        <div id="lianxirenbox" class="layui-input-inline">
                            <input type="text" name="truename" id="truename"
                                   autocomplete="off" placeholder="请输入甲方联系人"
                                   class="layui-input" value="<?php echo $projectInfo['truename']; ?>"></div>
                    </div>

                </div>
                <div class="layui-form-item">
                    <div class="layui-col-sm6"><label class="layui-form-label">联系方式</label>
                        <div class="layui-input-inline">

                            <input type="text" name="mobile"
                                   autocomplete="off" id="mobile" placeholder="请输入甲方联系方式"
                                   class="layui-input" value="<?php echo $projectInfo['mobile']; ?>"></div>
                    </div>
                    <div class="layui-col-sm6"><label class="layui-form-label">施工地址</label>
                        <div class="layui-input-block">

                            <input type="text" name="address"
                                   autocomplete="off" id="address" placeholder="请输入工程的施工地址"
                                   class="layui-input" value="<?php echo $projectInfo['address']; ?>"></div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-col-sm6"><label class="layui-form-label">施工区域</label>
                        <div class="layui-input-inline">
                            <input name="area" type="radio" value="city" title="市区" <?php if ($projectInfo['area'] == "city") {echo  "checked";}?> />
                            <input name="area" type="radio" value="town" title="县乡" <?php if ($projectInfo['area'] == "town") {echo  "checked";}?> />
                        </div>

                    </div>
                </div>



                <div class="layui-form-item layui-row">
                    <div class="layui-col-sm6"><label class="layui-form-label">开始时间<span class="red">*</span></label>
                        <div id="regionbox" class="layui-input-inline">
                            <input class="layui-input " name="beginTime" id="beginTime" type="text"
                                   onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd '})"
                                   value="<?php echo date("Y-m-d", $projectInfo['begintime']) ?>">
                        </div>
                    </div>
                    <div class="layui-col-sm6"><label class="layui-form-label">结束时间<span class="red">*</span></label>
                        <div id="regionbox" class="layui-input-inline">
                            <input class="layui-input" name="endTime" id="endTime" type="text"
                                   onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd '})"
                                   value="<?php echo date("Y-m-d ", $projectInfo['endtime']) ?>">
                        </div>
                    </div>


                </div>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-sm6"><label class="layui-form-label">验收开始时间<span class="red">*</span></label>
                        <div id="regionbox" class="layui-input-inline">
                            <input class="layui-input " name="oktime" id="oktime" type="text"
                                   onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd '})"
                                   value="<?php echo date("Y-m-d ", $projectInfo['oktime']>0 ? $projectInfo['oktime'] : time() ) ?>">
                        </div>
                    </div>
                    <div class="layui-col-sm6"><label class="layui-form-label">验收结束时间<span class="red">*</span></label>
                        <div id="regionbox" class="layui-input-inline">
                            <input class="layui-input " name="oktime2" id="oktime2" type="text"
                                   onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd'})"
                                   value="<?php echo date("Y-m-d ", $projectInfo['oktime2']>0 ? $projectInfo['oktime2'] : time() ) ?>">
                        </div>
                    </div>


                </div>

                <div class="layui-form-item"><label class="layui-form-label">施工要求<span class="red">*</span></label>
                    <div class="layui-input-block"><textarea name="content" lay-verify="required"
                                                             placeholder="请输入施工要求" class="layui-textarea"><?php echo $projectInfo['content']; ?></textarea>
                    </div>

                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label ">施工图纸</label>
                    <div class="layui-input-block" style="margin-left: 120px">
                        <a type="button" class="layui-btn layui-btn-danger"
                           id="test-upload-more">多图片上传
                        </a>
                        <blockquote class="layui-elem-quote layui-quote-nm"
                                    style="margin-top: 10px;">
                            预览图：
                            <div class="layui-upload-list"
                                 id="test-upload-more-list">
                                <?php
                                $picfiles=json_decode($projectInfo['picfiles'],true);
                                if(is_array($picfiles)) {
                                    foreach ($picfiles as $key => $val) {
                                        echo '<div class="layui-upload-img" id="'.$key.'"><img src="'.$cfg_cdn.$val.'"  class="layui-upload-img"><a  class="layui-btn layui-btn-sm layui-btn-normal" href="javascript:void(0);" onclick=ClearPicArr("'.$key .'")>删除</a><input type="hidden" name="picarr" value="'.$val .'" ></div>';
                                    }
                                }
                                ?>
                            </div>
                        </blockquote>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label ">施工计划</label>
                    <div class="layui-input-block" style="margin-left: 120px">
                        <a type="button" class="layui-btn layui-btn-success"
                           id="test-upload-more2">多图片上传
                        </a>
                        <blockquote class="layui-elem-quote layui-quote-nm"
                                    style="margin-top: 10px;">
                            预览图：
                            <div class="layui-upload-list"
                                 id="test-upload-more-list2">
                                <?php
                                $picfiles2=json_decode($projectInfo['planpicurls'],true);
                                if(is_array($picfiles2)) {
                                    foreach ($picfiles2 as $key => $val) {
                                        echo '<div class="layui-upload-img" id="'.$key.'"><img src="'.$cfg_cdn.$val.'"  class="layui-upload-img"><a  class="layui-btn layui-btn-sm layui-btn-normal" href="javascript:void(0);" onclick=ClearPicArr("'.$key .'")>删除</a><input type="hidden" name="picarr2" value="'.$val .'" ></div>';
                                    }
                                }
                                ?>
                            </div>
                        </blockquote>
                    </div>
                </div>



                <div class="layui-form-item">
                    <label class="layui-form-label">项目经理<span class="red">*</span></label>
                    <div class="layui-input-block">
                        <input name="qiang" type="hidden" value="p"/>
                        <blockquote class="layui-elem-quote">

                            <?php
                            $dosql->Execute("SELECT * FROM  `#@__user`    WHERE ispm=1  ORDER BY id asc");
                            while ($projectInfo3 = $dosql->GetArray()) {
                                $sec = "";
                                if ($projectInfo['pmid'] == $projectInfo3['id']) {
                                    $sec = "checked";
                                }
                                echo '<input '.$sec.'  type="radio"  name="pmid" title="' . $projectInfo3['truename'] . '" value="' . $projectInfo3['id'] . '"/>';
                            }
                            ?>

                        </blockquote>
                    </div>
                </div>

                <div class="layui-form-item" id="sub_box">
                    <div class="layui-input-inline">
                        <button class="layui-btn" lay-submit="" lay-filter="layuiadmin-order-submit">立即提交</button>
                        <button class="layui-btn layui-btn-primary" data-type="reset">重置</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'laydate', 'table', 'layedit', 'upload'], function () {
        var $ = layui.$,
            element = layui.element,
            layer = layui.layer,
            view = layui.view,
            table = layui.table,
            laytpl = layui.laytpl,
            laydate = layui.laydate,
            layedit = layui.layedit,
            upload = layui.upload,
            form = layui.form;



        form.render(null, 'component-form-group-edit');
        //监听提交
        form.on('submit(layuiadmin-order-submit)', function (data) {

            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();
            var d1 = new Date(beginTime.replace(/\-/g, "\/"));
            var d2 = new Date(endTime.replace(/\-/g, "\/"));
            console.log(d1);
            if (beginTime != "" && endTime != "" && d1 >= d2) {
                layer.msg("开始时间不能大于结束时间！");
                return false;
            }


            var field = data.field; //获取提交的字段
            //提交 Ajax 成功后，关闭当前弹层并重载表格


            var picarr = [];
            $('input[name="picarr"]').each(function () {
                console.log($(this).val());
                picarr.push($(this).val());
            });
            field.picarr_arr = picarr;
            console.log(picarr)

            var picarr2 = [];
            $('input[name="picarr2"]').each(function () {
                console.log($(this).val());
                picarr2.push($(this).val());
            });
            field.picarr_arr2 = picarr2;
            console.log(picarr)

            $.ajax({
                url: "json/project/project_do.php",
                data: {
                    "field": field,
                    "action": 'update'

                },
                type: "POST",
                dataType: "json",
                success: function (res) {
                    console.log(res);
                    if (res.code == 0) {
                        console.log("返回成功");
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭

                    } else {
                        layer.msg(res.msg);
                    }

                }
            });
        });


        //上传组图
        var filepaths = "";
        //多图片上传
        var upins = upload.render({
            elem: '#test-upload-more'
            , url: 'https://zmdtc.oss-cn-beijing.aliyuncs.com' //接口url
            , multiple: true
            , data: {
                key: function () {
                    let timeis = Date.parse(new Date());
                    let letter = Math.random().toString(36).substr(2);
                    return "uploads/" + timeis + letter + ".jpg";
                },
                'policy': '',
                'OSSAccessKeyId': '',
                'success_action_status': "200",
                'signature': ''
            }
            , before: function (obj) {
                console.log(obj);
                $.ajax({
                    url: 'oss/get.php',
                    data: {},
                    async: false,
                    dataType: "json",
                    success: function (res) {
                        console.log(res.policy);
                        upins.config.data.policy = res.policy;
                        upins.config.data.OSSAccessKeyId = res.accessid;
                        upins.config.data.signature = res.signature;
                        upins.config.data.callback = res.callback;
                        upins.config.data.success_action_status = '200'
                    }

                });
            }
            , done: function (res, index) {
                //上传完毕
                if (res.status == 'ok') {
                    console.log(res)
                    //上传完毕
                    $('#test-upload-more-list').append('<div class="layui-upload-img" id="' + index + '"><img src="' + res.data.src + '"  class="layui-upload-img"><a  class="layui-btn layui-btn-sm layui-btn-normal" href="javascript:void(0);" onclick=ClearPicArr("' + index + '")>删除</a><input type="hidden" name="picarr" value="' + res.data.filename + '" ></div>')
                    layui.form.render();
                }
            }
        });


        //上传组图
        var filepaths2 = "";
        //多图片上传
        var upins2 = upload.render({
            elem: '#test-upload-more2'
            , url: 'https://zmdtc.oss-cn-beijing.aliyuncs.com' //接口url
            , multiple: true
            , data: {
                key: function () {
                    let timeis = Date.parse(new Date());
                    let letter = Math.random().toString(36).substr(2);
                    return "uploads/" + timeis + letter + ".jpg";
                },
                'policy': '',
                'OSSAccessKeyId': '',
                'success_action_status': "200",
                'signature': ''
            }
            , before: function (obj) {
                console.log(obj);
                $.ajax({
                    url: 'oss/get.php',
                    data: {},
                    async: false,
                    dataType: "json",
                    success: function (res) {
                        console.log(res.policy);
                        upins2.config.data.policy = res.policy;
                        upins2.config.data.OSSAccessKeyId = res.accessid;
                        upins2.config.data.signature = res.signature;
                        upins2.config.data.callback = res.callback;
                        upins2.config.data.success_action_status = '200'
                    }

                });
            }
            , done: function (res, index) {
                //上传完毕
                if (res.status == 'ok') {
                    console.log(res)
                    //上传完毕
                    $('#test-upload-more-list2').append('<div class="layui-upload-img" id="' + index + '"><img src="' + res.data.src + '"  class="layui-upload-img"><a  class="layui-btn layui-btn-sm layui-btn-normal" href="javascript:void(0);" onclick=ClearPicArr("' + index + '")>删除</a><input type="hidden" name="picarr2" value="' + res.data.filename + '" ></div>')
                    layui.form.render();
                }
            }
        });


        layui.form.render();


    });

    //删除图片
    function ClearPicArr(id) {
        console.log(id);
        document.getElementById(id).innerHTML = "";
        document.getElementById(id).style.display = "none";
    }


</script>

</body>
</html>